import { ElMessage, ElMessageBox } from "element-plus";
import router from "@/router/router";
// 消息提示
export function messageTip(msg, type) {
  ElMessage({
    showClose: true,
    center: true,
    duration: 3000,
    message: msg,
    type: type
  })
}

//获取存储在sessionStorage或者localStrorage中的token（jwt）的名字 
export function getTokenName() {
  return "dlykJWT"
}

//删除
export function removeToken() {
  window.sessionStorage.removeItem(getTokenName())
  window.localStorage.removeItem(getTokenName())
}

//消息确认提示
export function messageConfirm(msg) {
  return ElMessageBox.confirm(
    msg,
    "warning",
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: "warning"
    }
  )
}
export function goBack() {
  router.go(-1);
}

export function getToken() {
  //从sessionStorage获取token
  let token = window.sessionStorage.getItem(getTokenName())
  if (!token) {
    //从localStorage获取token
    token = window.localStorage.getItem(getTokenName())
  }

  if (token) {
    return token;
  } else {
    messageConfirm("请求token未空,是否重新去登录").then(() => {
      //点击确定跳转到登录页面
      removeToken();
      window.location.href = "/";
    }).catch(() => {
      messageTip("取消去登录", "warning")
    })
  }
}
